package com.example.minefieldkeepercloud.activity.commonpool;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.adapter.PoolDetailAdapter;
import com.example.minefieldkeepercloud.adapter.SetAdapter;
import com.example.minefieldkeepercloud.bean.PoolBean;
import com.example.minefieldkeepercloud.bean.PoolDetailBean;
import com.example.minefieldkeepercloud.bean.SetPoolBean;
import com.example.minefieldkeepercloud.interfac.ApiHttpCallback;
import com.example.minefieldkeepercloud.network.HttpUtils;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.NetWorkUtil;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 设置矿池地址
 */
public class SetPool extends AppCompatActivity {
    private String ttkh="",factoryId="",smsSwitch="",phone="",ltPool="";
    private PoolDetailBean poolDetailBean;
    private SetAdapter setAdapter;
    private List<PoolBean> listData;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    @BindView(R.id.recyleview)
    LRecyclerView mRecyclerView;
    @BindView(R.id.iv_add)
    public ImageView iv_add;
    private TextView edit_pool,edit_createPerson,edit_customer;
    private EditText edit_phone;
    private Switch aSwitch;

    public static void start(Activity mactivity, String Id) {
        Intent intent = new Intent(mactivity, SetPool.class);
        intent.putExtra("Id", Id);
        mactivity.startActivity(intent);

    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_setpool);
        ButterKnife.bind(this);
        initview();
        getNetData();
    }

    private void initview()
    {
//        bean = (PoolDetailBean) getIntent().getBundleExtra("bd").getSerializable("data");
        listData=new ArrayList<>();
        setAdapter=new SetAdapter(this,listData);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(setAdapter);
        View header = LayoutInflater.from(this).inflate(R.layout.sample_set_header,(ViewGroup)findViewById(android.R.id.content), false);
        edit_pool=header.findViewById(R.id.edit_pool);
        edit_createPerson=header.findViewById(R.id.edit_createPerson);
        edit_customer=header.findViewById(R.id.edit_customer);
        edit_phone=header.findViewById(R.id.edit_phone);
        aSwitch=header.findViewById(R.id.switch_state);
        lRecyclerViewAdapter.addHeaderView(header);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(lRecyclerViewAdapter);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader); //设置下拉刷新Progress的样式
        // mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);  //设置下拉刷新箭头
        //设置头部加载颜色
        mRecyclerView.setHeaderViewColor(R.color.system_color, R.color.system_color, R.color.bg);
//设置底部加载颜色
        mRecyclerView.setFooterViewColor(R.color.system_color, R.color.system_color, android.R.color.white);
        mRecyclerView.setPullRefreshEnabled(false);
        mRecyclerView.setLoadMoreEnabled(false);
//        CreateData();
    }

    private void CreateData()
    {
        edit_pool.setText(poolDetailBean.getData().getKC());
        edit_createPerson.setText(poolDetailBean.getData().getCJR());
        edit_customer.setText(poolDetailBean.getData().getTGKH());
        edit_phone.setText(poolDetailBean.getData().getMobilePhone());
        if(poolDetailBean.getData().getSMSSwitch()==0)
        {
            aSwitch.setChecked(false);
        }
        else
        {
            aSwitch.setChecked(true);
        }
//        if(poolDetailBean.getData().getPool().size()>0)
//        {
//            iv_add.setVisibility(View.GONE);
//        }
//        else
//        {
//            iv_add.setVisibility(View.VISIBLE);
//        }
        for(Map.Entry<String, String> entry :poolDetailBean.getData().getPool().entrySet()){
            String str[];
            str=entry.getKey().split("\\|");
            PoolBean poolBean=new PoolBean();
            poolBean.setKC(str[1]);
            poolBean.setKGM(str[0]);
            poolBean.setID(entry.getValue());
            listData.add(poolBean);
        }
        setAdapter.notifyDataSetChanged();
    }

    @OnClick({R.id.title_left,R.id.btn_add,R.id.iv_add})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left:
                finish();
                break;
            case R.id.iv_add:
                setAdapter.add();
                break;
            case R.id.btn_add:
                  if(aSwitch.isChecked())
                  {
                      smsSwitch="1";
                  }
                  else
                  {
                      smsSwitch="0";
                  }
                  if(setAdapter.getMap()==null)
                  {
                      ToastUtil.showToast(SetPool.this,"请至少添加一组矿池及矿工");
                      return;
                  }
                Log.i("-->","提交矿池记旷工："+setAdapter.getMap().toString());
                  NetHelperNew.SetPool(poolDetailBean.getData().getTGID()+"", poolDetailBean.getData().getFactoryID()+"", smsSwitch ,edit_phone.getText().toString(), setAdapter.getMap(), new ApiHttpCallback(SetPool.this,0,true) {
                    @Override
                    public void onSuccess(String data) {
                        SetPoolBean setPoolBean = MyGson.getInstance().fromJson(data, SetPoolBean.class);
                        if(setPoolBean.getType()==1)
                        {
                             EventBus.getDefault().post("10115");
//                            finish();
                        }
                        ToastUtil.showToast(SetPool.this,setPoolBean.getContent());
                    }
                });

                break;
        }
    }

    private void getNetData()
    {

        if (!NetWorkUtil.check(this)) {

            return;
        }
        NetHelperNew.GetWhiteOrder(getIntent().getStringExtra("Id"),new ApiHttpCallback(SetPool.this,1,true) {
            @Override
            public void onSuccess(String data) {
                poolDetailBean = MyGson.getInstance().fromJson(data, PoolDetailBean.class);
                if (poolDetailBean.getType() == 1) {
//                    tv_pool.setText(poolDetailBean.getData().getKC());
//                    tv_createPerson.setText(poolDetailBean.getData().getTGKH());
//                    tv_customer.setText(poolDetailBean.getData().getMobilePhone());
//
//                    if(poolDetailBean.getData().getSMSSwitch()==0)
//                    {
//                        tv_state.setText("关闭");
//                        tv_state.setTextColor(getResources().getColor(R.color.red));
//                    }
//                    else
//                    {
//                        tv_state.setText("开启");
//                        tv_state.setTextColor(getResources().getColor(R.color.system_color));
//                    }
//
//                    for(Map.Entry<String, String> entry :poolDetailBean.getData().getPool().entrySet()){
//                        String str[];
//                        str=entry.getKey().split("\\|");
//                        PoolBean poolBean=new PoolBean();
//                        poolBean.setKC(str[1]);
//                        poolBean.setKGM(str[0]);
//                        listData.add(poolBean);
//                    }
//                    setAdapter.notifyDataSetChanged();
                    CreateData();

                } else {
                    ToastUtil.showToast(SetPool.this, poolDetailBean.getContent());
                }
                mRecyclerView.refreshComplete(10);
            }


        });



    }
}
